import path, { resolve } from 'node:path'
import vue from '@vitejs/plugin-vue'
import VueJsx from '@vitejs/plugin-vue-jsx'
import reactivityTransform from '@vue-macros/reactivity-transform/vite'
import Unocss from 'unocss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { defineConfig } from 'vite'
import Inspector from 'vite-plugin-vue-inspector'
// https://vite.dev/config/
export default defineConfig({
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@use "~/styles/element/index.scss" as *;`,
        api: 'modern-compiler',
      },
    },
  },
  plugins: [
    Inspector({
      toggleButtonPos: 'top-left',
    }),
    vue(),
    VueJsx(),
    Unocss(),
    reactivityTransform(),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        'pinia',
        '@vueuse/core',
        {
          '@/constant/DialogAction': ['DialogActionMap'],
        },
        {
          dayjs: [['default', 'dayjs']],
        },
      ],
      dirs: ['src/composables', 'src/stores/modules', 'src/hook'],
      resolvers: [ElementPlusResolver()],
      dts: 'src/typings/auto-imports.d.ts',
    }),
    Components({
      resolvers: [ElementPlusResolver({
        importStyle: 'sass',
      })],
      dts: 'src/typings/components.d.ts',
    }),
  ],
  resolve: {
    alias: {
      '~/': `${path.resolve(__dirname, 'src')}/`,
      '@/': '/src/',
      '#/': '/src/typings/',
    },
  },
  base: '/',
  build: {
    chunkSizeWarningLimit: 1000,
    minify: false,
    outDir: './dist',
    rollupOptions: {
      input: {
        main: resolve(__dirname, 'index.html'),
      },
    },
  },
  server: {
    proxy: {
      '/api': {
        target: 'http://localhost:8000',
        changeOrigin: true,
        rewrite: path => path.replace(/^\/api/, ''),
      },
    },
  },
})
